package backend

import (
	"awesomeProject1/pkg/e"
	"awesomeProject1/pkg/gintool"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/simplechain-org/client/log"
	"time"
)

func getSlsBanks() map[string]struct{} {
	slsBanks := map[string]struct{}{
		"浙江农村商业联合银行":     {},
		"杭州联合农村商业银行":     {},
		"浙江萧山农村商业银行":     {},
		"浙江杭州余杭农村商业银行":   {},
		"浙江富阳农村商业银行":     {},
		"浙江临安农村商业银行":     {},
		"浙江桐庐农村商业银行":     {},
		"浙江建德农村商业银行":     {},
		"浙江淳安农村商业银行":     {},
		"宁波慈溪农村商业银行":     {},
		"宁波余姚农村商业银行":     {},
		"宁波北仑农村商业银行":     {},
		"宁波奉化农村商业银行":     {},
		"象山县农村信用合作联社":    {},
		"宁波镇海农村商业银行":     {},
		"宁波宁海农村商业银行":     {},
		"宁波甬城农村商业银行":     {},
		"浙江温州鹿城农村商业银行":   {},
		"浙江温州龙湾农村商业银行":   {},
		"浙江温州瓯海农村商业银行":   {},
		"浙江温州洞头农村商业银行":   {},
		"浙江乐清农村商业银行":     {},
		"浙江永嘉农村商业银行":     {},
		"浙江瑞安农村商业银行":     {},
		"浙江平阳农村商业银行":     {},
		"浙江苍南农村商业银行":     {},
		"浙江文成农村商业银行":     {},
		"浙江泰顺农村商业银行":     {},
		"浙江龙港农村商业银行":     {},
		"浙江禾城农村商业银行":     {},
		"浙江嘉善农村商业银行":     {},
		"浙江平湖农村商业银行":     {},
		"浙江海宁农村商业银行":     {},
		"浙江海盐农村商业银行":     {},
		"浙江桐乡农村商业银行":     {},
		"浙江德清农村商业银行":     {},
		"浙江长兴农村商业银行":     {},
		"浙江安吉农村商业银行":     {},
		"湖州吴兴农村商业银行":     {},
		"浙江南浔农村商业银行":     {},
		"浙江绍兴瑞丰农村商业银行":   {},
		"浙江上虞农村商业银行":     {},
		"浙江嵊州农村商业银行":     {},
		"浙江新昌农村商业银行":     {},
		"浙江诸暨农村商业银行":     {},
		"浙江绍兴恒信农村商业银行":   {},
		"浙江金华成泰农村商业银行":   {},
		"浙江兰溪农村商业银行":     {},
		"浙江永康农村商业银行":     {},
		"浙江武义农村商业银行":     {},
		"浙江东阳农村商业银行":     {},
		"浙江磐安农村商业银行":     {},
		"浙江义乌农村商业银行":     {},
		"浙江浦江农村商业银行":     {},
		"浙江衢州柯城农村商业银行":   {},
		"浙江衢州衢江农村商业银行":   {},
		"浙江龙游农村商业银行":     {},
		"浙江江山农村商业银行":     {},
		"浙江常山农村商业银行":     {},
		"浙江开化农村商业银行":     {},
		"浙江丽水莲都农村商业银行":   {},
		"浙江青田农村商业银行":     {},
		"浙江云和农村商业银行":     {},
		"浙江龙泉农村商业银行":     {},
		"浙江庆元农村商业银行":     {},
		"浙江缙云农村商业银行":     {},
		"浙江遂昌农村商业银行":     {},
		"浙江松阳农商银行":       {},
		"浙江景宁农村商业银行":     {},
		"浙江台州椒江农村商业银行":   {},
		"浙江台州黄岩农村商业银行":   {},
		"浙江台州路桥农村商业银行":   {},
		"浙江临海农村商业银行":     {},
		"浙江温岭农村商业银行":     {},
		"浙江玉环农村商业银行":     {},
		"浙江天台农村商业银行":     {},
		"浙江仙居农村商业银行":     {},
		"浙江三门农村商业银行":     {},
		"浙江舟山定海海洋农村商业银行": {},
		"浙江舟山普陀农村商业银行":   {},
		"浙江岱山农村商业银行":     {},
		"浙江嵊泗农村商业银行":     {},
	}
	return slsBanks
}

func parseDateRange(ctx *gin.Context, startDate, endDate string) (hasEnd bool, startTime, endTime time.Time) {
	if startDate == "" || endDate == "" {
		hint := fmt.Sprintf("必须输入开始日期和结束日期")
		log.Error("params validate error:必须输入开始日期和结束日期", "startDate", startDate, "endDate", endDate)
		gintool.ResultCodeWithData(ctx, e.InvalidParams, hint)
		return
	}

	startTime, err := time.ParseInLocation("20060102", startDate, time.Local)
	if err != nil {
		hint := fmt.Sprintf("开始日期格式错误：示例：20230707, err:%v", err.Error())
		log.Error(hint, "startTime", startTime)
		gintool.ResultCodeWithData(ctx, e.InvalidParams, hint)
		return
	}
	endTime, err = time.ParseInLocation("20060102", endDate, time.Local)
	if err != nil {
		hint := fmt.Sprintf("结束日期格式错误：示例：20230707, err:%v", err.Error())
		log.Error(hint, "endTime", endTime)
		gintool.ResultCodeWithData(ctx, e.InvalidParams, hint)
		return
	}
	endTime = endTime.Add(time.Hour * 24)
	if startTime.After(endTime) {
		hint := fmt.Sprintf("结束日期早于开始日期")
		log.Error(hint, "startDate", startDate, "endDate", endDate)
		gintool.ResultCodeWithData(ctx, e.InvalidParams, hint)
		return
	}

	if endTime.Sub(startTime).Hours() > 24*66 {
		hint := fmt.Sprintf("最多两个月的数据")
		log.Error(hint, "startDate", startDate, "endDate", endDate)
		gintool.ResultCodeWithData(ctx, e.InvalidParams, hint)
		return
	}
	return
}
